package com.sixmac.controller;

import com.sixmac.common.base.BaseController;
import com.sixmac.core.plugins.dao.Blade;
import com.sixmac.core.toolbox.ajax.AjaxResult;
import com.sixmac.core.toolbox.kit.JsonKit;
import com.sixmac.model.RoomApplication;
import com.sixmac.model.RoomInstall;
import com.sixmac.service.RoomApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * RoomApplicationController
 * 2018-09-29 18:11:54
 *
 * @author Blade
 */
@Controller
@RequestMapping("/roomApplication")
public class RoomApplicationController extends BaseController {
    private static String CODE = "roomApplication";
    private static String PREFIX = "room_application";
    private static String LIST_SOURCE = "roomApplication.list";
    private static String BASE_PATH = "/platform/roomApplication/";

    @Autowired
    RoomApplicationService service;

    @RequestMapping(KEY_MAIN)
    public String index(ModelMap mm) {
        mm.put("code", CODE);
        return BASE_PATH + "roomApplication.html";
    }

    @RequestMapping("/allot")
    public String allot(ModelMap mm, Integer rid) {
        mm.put("code", CODE);
        mm.put("rid", rid);
        return BASE_PATH + "allot.html";
    }

    @RequestMapping("/saveAllot")
    @ResponseBody
    public AjaxResult saveAllot(Integer rid, Integer allotId) {
        if (allotId == null || rid == null)
            return error("id不存在");
        RoomApplication roomApplication = service.findById(rid);
        if (roomApplication == null)
            return error("申请不存在");
        RoomInstall roomInstall = Blade.create(RoomInstall.class).findFirstBy("applicationId", roomApplication.getId());
        if (roomInstall == null) {
            roomInstall = new RoomInstall();
            roomInstall.setApplicationId(rid);
        }
        roomInstall.setUserId(allotId);
        Blade.create(RoomInstall.class).saveOrUpdate(roomInstall);

        return success("分配成功");
    }

    @RequestMapping(KEY_ADD)
    public String add(ModelMap mm) {
        mm.put("code", CODE);
        return BASE_PATH + "roomApplication_add.html";
    }

    @RequestMapping(KEY_EDIT + "/{id}")
    public String edit(@PathVariable Integer id, ModelMap mm) {
        RoomApplication roomApplication = service.findById(id);
        mm.put("model", JsonKit.toJson(roomApplication));
        mm.put("id", id);
        mm.put("code", CODE);
        return BASE_PATH + "roomApplication_edit.html";
    }

    @RequestMapping(KEY_VIEW + "/{id}")
    public String view(@PathVariable Integer id, ModelMap mm) {
        RoomApplication roomApplication = service.findById(id);
        mm.put("model", JsonKit.toJson(roomApplication));
        mm.put("id", id);
        mm.put("code", CODE);
        return BASE_PATH + "roomApplication_view.html";
    }

    @ResponseBody
    @RequestMapping(KEY_LIST)
    public Object list() {
        Object grid = paginate(LIST_SOURCE);
        return grid;
    }

    @ResponseBody
    @RequestMapping(KEY_SAVE)
    public AjaxResult save() {
        RoomApplication roomApplication = mapping(PREFIX, RoomApplication.class);
        boolean temp = service.save(roomApplication);
        if (temp) {
            return success(SAVE_SUCCESS_MSG);
        } else {
            return error(SAVE_FAIL_MSG);
        }
    }

    @ResponseBody
    @RequestMapping(KEY_UPDATE)
    public AjaxResult update() {
        RoomApplication roomApplication = mapping(PREFIX, RoomApplication.class);
        boolean temp = service.update(roomApplication);
        if (temp) {
            return success(UPDATE_SUCCESS_MSG);
        } else {
            return error(UPDATE_FAIL_MSG);
        }
    }

    @ResponseBody
    @RequestMapping(KEY_REMOVE)
    public AjaxResult remove(@RequestParam String ids) {
        int cnt = service.deleteByIds(ids);
        if (cnt > 0) {
            return success(DEL_SUCCESS_MSG);
        } else {
            return error(DEL_FAIL_MSG);
        }
    }
}
